package com.yequan.hbb.adapter;



import android.content.Context;
import android.net.Uri;
import android.support.v4.util.Pools;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.yequan.hbb.entries.FocusBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gwf on 2016/10/14.
 */

public class HPCPagerAdapter extends PagerAdapter {
    private List<FocusBean.DataBean.CategoryBean.ItemBean> list;
    private Context context;
    private Pools.Pool<SimpleDraweeView> pool;

    public HPCPagerAdapter(Context context, List<FocusBean.DataBean.CategoryBean.ItemBean> list) {
        this.context = context;
        this.list = new ArrayList<>(list);
        pool = new Pools.SimplePool<>(list.size());
        FocusBean.DataBean.CategoryBean.ItemBean first = this.list.get(0);
        FocusBean.DataBean.CategoryBean.ItemBean last = this.list.get(list.size() - 1);
        this.list.add(0,last);
        this.list.add(first);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        SimpleDraweeView imageView = pool.acquire();
        if (imageView == null) {
            imageView = new SimpleDraweeView(context);
        }
        Uri uri = Uri.parse(list.get(position).getImage());
        imageView.setImageURI(uri);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        pool.release((SimpleDraweeView) object);
    }
}
